package com.brunobas.pontosmart.utils;

import com.brunobas.pontosmart.R;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Util {

	public static final int TIPO_IMG_TOAST_INFO = 0; 
	public static final int TIPO_IMG_TOAST_WARG = 1; 
	public static final int TIPO_IMG_TOAST_ERRO = 2; 
	
	/**
	 * Retorna {@link LayoutInflater}
	 * 
	 * @param context
	 * @return
	 */
	public static LayoutInflater getLayoutInflater(Context context) {
		
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		return inflater;
	}
	
	/**
	 * Custom Toast show
	 * 
	 * @param context
	 * @param tpImg
	 * @param makeText
	 */
	public static void showToastCustom(Context context, int tpImg, String makeText) {
		
		Toast toastCustom = getToastCustom(context, tpImg, makeText);
		
		toastCustom.show();
	}
	
	
	/**
	 * Retorna um Toast Customizado
	 * 
	 * @param context
	 * @param tpImg
	 * @param makeText
	 * @return
	 */
	public static Toast getToastCustom(Context context, int tpImg, String makeText) {
		
		Toast toastCustom = null;
		
		switch (tpImg) 
		{
			case TIPO_IMG_TOAST_INFO:
				
				toastCustom = getToastCustomizado(context, makeText, R.drawable.icone_info);
				
				break;
				
			case TIPO_IMG_TOAST_WARG:
				
				toastCustom = getToastCustomizado(context, makeText, R.drawable.icone_warg);
				
				break;
				
			case TIPO_IMG_TOAST_ERRO:
				
				toastCustom = getToastCustomizado(context, makeText, R.drawable.icone_erro);
				
				break;
		}
		
		return toastCustom;
	}
	
	private static Toast getToastCustomizado(Context context, String makeText, int idImg) {

		Toast toastCustom = new Toast(context);
		toastCustom.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
		toastCustom.setDuration(Toast.LENGTH_LONG);
		toastCustom.setView(getViewLayoutToastCustom(context, makeText, idImg));
		
		return toastCustom;
	}


	private static View getViewLayoutToastCustom(Context context, String makeText, int idImg) {
		
		LayoutInflater inflater = getLayoutInflater(context);
		
		View layout = (View) inflater.inflate(R.layout.layout_toast, null);
		
		ImageView imageView = (ImageView) layout.findViewById(R.id.imageToastCustom);
		
		TextView textView = (TextView) layout.findViewById(R.id.textViewToastCustom);
		
		imageView.setImageResource(idImg);
		
		textView.setText(makeText);
		
		return layout;
	}
}
